package com.example.hospitalManage.controller.admin.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hospitalManage.entity.Dict;
import com.example.hospitalManage.entity.Hospital;
import com.example.hospitalManage.service.DictService;
import com.example.hospitalManage.service.HospitalService;
import com.example.hospitalManage.utils.Response;
import com.example.hospitalManage.utils.ResponseResult;
import com.example.hospitalManage.vo.HospitalQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description:
 * Param:
 * return:
 * Author: 彭雨胜
 * Data: 2022/12/30
 */
@RestController
@RequestMapping("/admin")
@Api(value = "v1",tags = "医院详情相关接口")
@CrossOrigin
public class HospitalController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private DictService dictService;

    //定义日志对象
    public final static Logger log = LoggerFactory.getLogger(HospitalController.class);

    // 查询医院详情表所有信息
    @ApiOperation(value = "获取所有医院详情")
    @GetMapping("hospital")
    public ResponseResult<List<Hospital>> findAllHospital() {
        List<Hospital> hospitalList = hospitalService.list();
        return Response.createOkResp(hospitalList);
    }

    // 根据id获取医院详情
    @ApiOperation(value = "根据id获取医院详情")
    @GetMapping("hospital/{id}")
    public ResponseResult showHospitalById(@PathVariable Long id) {
        Hospital hospital = hospitalService.getById(id);

        String hosname2 = hospital.getHosname();
        String provinceCode2 = hospital.getProvinceCode();
        String cityCode2 = hospital.getCityCode();
        String districtCode2 = hospital.getDistrictCode();
        String hostype2 = hospital.getHostype();
        String hostypeString = "";
        if(!StringUtils.isEmpty(provinceCode2)) {
            hostypeString = getHosTypeByCode(hostype2);
        }
        hospital.setHostypeString(hostypeString);

        // 设置医院地址
        // 查询省编码的值
        String province = "";
        String city = "";
        String distinct = "";
        if(!StringUtils.isEmpty(provinceCode2)) {
            province = getNameByCode(provinceCode2);
        }
        if(!StringUtils.isEmpty(cityCode2)) {
            city = getNameByCode(cityCode2);
        }
        if(!StringUtils.isEmpty(districtCode2)) {
            distinct = getNameByCode(districtCode2);
        }

        hospital.setFullAddress(province + city + distinct + hospital.getAddress() + hosname2);
        return Response.createOkResp(hospital);
    }

    // 添加医院详情
    @ApiOperation(value = "添加医院详情")
    @PostMapping("hospital")
    public ResponseResult saveHospital(@RequestBody Hospital hospital) {
        hospital.setStatus(0);
        hospital.setIsDeleted(0);
        // 调用service的save方法
        boolean save = hospitalService.save(hospital);
        if(save) {
            return Response.createOkResp();
        } else {
            return Response.createFailResp();
        }
    }

    // 修改医院详情
    @PutMapping("hospital")
    @ApiOperation(value = "修改医院详情")
    public ResponseResult updateHospital(@RequestBody Hospital hospital) {
        boolean flag = hospitalService.updateById(hospital);
        if(flag) {
            return Response.createOkResp();
        } else {
            return Response.createFailResp();
        }
    }

    // 根据id删除医院设置
    @ApiOperation(value = "根据id删除医院详情")
    @DeleteMapping("hospital/{id}")
    public ResponseResult removeHospitalById(@PathVariable Long id) {
        boolean flag = hospitalService.removeById(id);
        if(flag) {
            return Response.createOkResp();
        } else {
            return Response.createFailResp("医院详情删除失败");
        }
    }

    // 条件查询带分页
    @ApiOperation(value = "条件分页查询")
    @PostMapping("hospital/{current}/{limit}")
    public ResponseResult findPageHospSet(@PathVariable long current,
                                          @PathVariable long limit,
                                          @RequestBody(required = false) HospitalQueryVo hospitalQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<Hospital> page = new Page<>(current, limit);
        //构建条件
        QueryWrapper<Hospital> wrapper = new QueryWrapper<>();
        String hosname = hospitalQueryVo.getHosname();  // 医院名称
        String hoscode = hospitalQueryVo.getHoscode();  // 医院编号
        String hostype = hospitalQueryVo.getHostype();  // 医院类型
        String provinceCode = hospitalQueryVo.getProvinceCode();  // 省code
        String cityCode = hospitalQueryVo.getCityCode();  // 市code
        String districtCode = hospitalQueryVo.getDistrictCode();  // 区code

        if(!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname",hosname);
        }
        if(!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode",hoscode);
        }
        if(!StringUtils.isEmpty(provinceCode)) {
            wrapper.eq("province_code",provinceCode);
        }
        if(!StringUtils.isEmpty(cityCode)) {
            wrapper.eq("city_code",cityCode);
        }
        if(!StringUtils.isEmpty(districtCode)) {
            wrapper.eq("district_code",districtCode);
        }
        if(!StringUtils.isEmpty(hostype)) {
            wrapper.eq("hostype",hostype);
        }

        //调用方法实现分页查询
        IPage<Hospital> pageHospital = hospitalService.page(page, wrapper);
        for (Hospital hospital: pageHospital.getRecords()) {
            String hosname2 = hospital.getHosname();
            String provinceCode2 = hospital.getProvinceCode();
            String cityCode2 = hospital.getCityCode();
            String districtCode2 = hospital.getDistrictCode();
            String hostype2 = hospital.getHostype();

            // 设置医院类型（医院等级）
            String hostypeString = "";
            if(!StringUtils.isEmpty(provinceCode2)) {
                hostypeString = getHosTypeByCode(hostype2);
            }
            hospital.setHostypeString(hostypeString);

            // 设置医院地址
            // 查询省编码的值
            String province = "";
            String city = "";
            String distinct = "";
            if(!StringUtils.isEmpty(provinceCode2)) {
                province = getNameByCode(provinceCode2);
            }
            if(!StringUtils.isEmpty(cityCode2)) {
                city = getNameByCode(cityCode2);
            }
            if(!StringUtils.isEmpty(districtCode2)) {
                distinct = getNameByCode(districtCode2);
            }

            hospital.setFullAddress(province + city + distinct + hospital.getAddress() + hosname2);
        }

        //返回结果
        return Response.createOkResp(pageHospital);
    }

    public String getHosTypeByCode(String code) {
        QueryWrapper<Dict> wrapperTmp = new QueryWrapper<>();
        wrapperTmp.eq("value",code);
        wrapperTmp.eq("parent_id",10000);
        Dict dictQuery = dictService.getOne(wrapperTmp);
        return dictQuery.getName();
    }

    public String getNameByCode(String code) {
        QueryWrapper<Dict> wrapperTmp = new QueryWrapper<>();
        wrapperTmp.eq("id",code);
        Dict dictQuery = dictService.getOne(wrapperTmp);
        return dictQuery.getName();
    }
}
